import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings.dev")

import django

django.setup()

import time

from django.conf import settings
from django.template import loader

from meiduo_mall.utils.my_category import get_categories, get_contents


def generate_static_index_html():
    """
    生成静态的主页面
    """

    print('%s: generate_static_index_html' % time.ctime())

    # 1,获取商品频道和分类
    cateagories = get_categories()

    contents = get_contents()

    # 渲染模板
    context = {
        'categories': cateagories,
        'contents': contents
    }

    index_html = loader.get_template('index.html')
    # 渲染首页html字符串
    index_html = index_html.render(context)
    # 将首页html字符串写入到指定目录，命名'index.html'
    with open(os.path.join(settings.STATICFILES_DIRS[0], 'index.html'), 'w', encoding='utf-8') as f:
        f.write(index_html)


if __name__ == "__main__":
    generate_static_index_html()
